package com.fenghongzhang.day016;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity {

    private List<SongCustom> list ;
    private static final String TAG = "Main4Activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        list = new ArrayList<>();
        new MyTask1().execute();
    }

    class MyTask1 extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url=new URL("http://musicapi.leanapp.cn/search/suggest?keywords=zhoujielun");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                if(connection.getResponseCode()==200){
                    StringBuilder stringBuilder = new StringBuilder();
                    InputStream inputStream  = connection.getInputStream();
                    byte[] b=new byte[1024];
                    int len=0;
                    while((len=inputStream.read(b))!=-1){
                        stringBuilder.append(new String(b,0,len));
                    }
                    String string = stringBuilder.toString();
                    return string;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Songs songs = gson.fromJson(s, Songs.class);
            List<Songs.ResultBean.SongsBean> songs1 = songs.getResult().getSongs();
            for (int i = 0; i < songs1.size(); i++) {
                Songs.ResultBean.SongsBean songsBean = songs1.get(i);
                int id = songsBean.getId();
                String name = songsBean.getName();
                SongCustom songCustom = new SongCustom();
                songCustom.setName(name);
                songCustom.setId(id);
                list.add(songCustom); //没有网址
                new MyTask2().execute(id+"");
            }
        }
    }

    class MyTask2 extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url=new URL("http://musicapi.leanapp.cn/music/url?id="+strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                if(connection.getResponseCode()==200){
                    StringBuilder stringBuilder = new StringBuilder();
                    InputStream inputStream  = connection.getInputStream();
                    byte[] b=new byte[1024];
                    int len=0;
                    while((len=inputStream.read(b))!=-1){
                        stringBuilder.append(new String(b,0,len));
                    }
                    String string = stringBuilder.toString();
                    return string;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Song song = gson.fromJson(s, Song.class);
            List<Song.DataBean> data = song.getData();
            for (int i = 0; i < data.size(); i++) {
                Song.DataBean dataBean = data.get(i);
                String url = dataBean.getUrl();

                int id = dataBean.getId();
                for (int j = 0; j < list.size(); j++) {
                    SongCustom songCustom = list.get(j);
                    if(songCustom.getId() == id ){
                        songCustom.setPath(url);
                    }
                    list.set(i,songCustom);
                }
//                Log.i(TAG, "onPostExecute: "+url);
//
                for (int j = 0; j < list.size(); j++) {
                    SongCustom songCustom = list.get(j);
                    Log.i(TAG, "onPostExecute: "+songCustom);

                }

            }
        }
    }
}
